<?php

namespace App\Models;

use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * 订单
 * Class Order
 *
 * @package App\Models
 * @property int $id
 * @property int $order_id
 * @property int $unique
 * @property int $uid
 * @property int $type 1：商家订单  2：普通订单
 * @property int|null $from_shop_id
 * @property string $from_name
 * @property string $from_phone
 * @property string $from_address
 * @property int|null $to_school_id
 * @property string $to_address
 * @property float $pay_price
 * @property int $paid
 * @property int|null $pay_time
 * @property string $pay_type
 * @property int $add_time
 * @property int $finish_time
 * @property int $status 0:未支付 1：支付 待接单 2：接单 3：送达
 * @property string|null $remark
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property int|null $deleted_at
 * @method static \Illuminate\Database\Eloquent\Builder|Order newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Order newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Order query()
 * @method static \Illuminate\Database\Eloquent\Builder|Order whereAddTime($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Order whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Order whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Order whereFinishTime($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Order whereFromAddress($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Order whereFromName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Order whereFromPhone($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Order whereFromShopId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Order whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Order whereOrderId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Order wherePaid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Order wherePayPrice($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Order wherePayTime($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Order wherePayType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Order whereRemark($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Order whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Order whereToAddress($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Order whereToSchoolId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Order whereType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Order whereUid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Order whereUnique($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Order whereUpdatedAt($value)
 * @mixin \Eloquent
 * @property int|null $escort_receive_time 镖师接收时间
 * @property int|null $escort_id 镖师
 * @property int|null $escort_finish_time 镖师配送完成时间
 * @method static \Illuminate\Database\Eloquent\Builder|Order whereEscortFinishTime($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Order whereEscortId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Order whereEscortReceiveTime($value)
 * @property string|null $to_pic 目的地图片
 * @method static \Illuminate\Database\Eloquent\Builder|Order whereToPic($value)
 * @property string|null $transaction_id
 * @property-read mixed $pay_type_text
 * @method static \Illuminate\Database\Eloquent\Builder|Order whereTransactionId($value)
 * @property-read mixed $status_text
 * @property-read \App\Models\School|null $school
 * @property-read \App\Models\Escort|null $escort
 * @property array|null $order_extend
 * @property string|null $out_trade_no
 * @property string|null $to_name
 * @property string $to_phone
 * @property-read mixed $type_text
 * @property-read \App\Models\User $user
 * @method static \Illuminate\Database\Eloquent\Builder|Order whereOrderExtend($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Order whereOutTradeNo($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Order whereToName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Order whereToPhone($value)
 * @property int|null $notify_times 通知次数
 * @property int|null $notify_complete 通知完成
 * @method static \Illuminate\Database\Eloquent\Builder|Order whereNotifyComplete($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Order whereNotifyTimes($value)
 */
class Order extends BaseModel
{
    use SoftDeletes;
    protected $guarded = [];
    protected $dateFormat = 'U';
    protected static $orderInfos;
    protected $hidden = ['deleted_at', 'updated_at', 'created_at', 'transaction_id', 'unique'];
    protected $appends = ['pay_type_text', 'status_text','type_text'];
    protected $casts = ['order_extend'=>'json'];

    protected static $payType = [
        'yue' => '余额',
        'weixin' => '微信'
    ];

    protected static $statusEnum = [
        1 => '待接单',
        2 => '配送中',
        3 => '已完成'
    ];
    protected static $types = [
        1 => '堂食',
        2 => '外卖订单',
        3 => '快捷买票'
    ];

    /**
     * 通过order id获取订单
     * @param $orderId
     */
    public static function getByOrderId($orderId)
    {
        $result = null;
        if (!$orderId) {
            return;
        }

        $result = self::whereOrderId($orderId)->first();

        self::$orderInfos[$orderId] = $result;

        return $result;
    }

    public function escort()
    {
        return $this->hasOne(Escort::class, 'id', 'escort_id');
    }

    public function getPayTypeTextAttribute()
    {
        return self::$payType[$this->pay_type];
    }

    public function getPayTimeAttribute($value)
    {
        if($value) {
            return date('Y-m-d H:i:s', $value);
        }
    }

    public function getAddTimeAttribute($value)
    {
        if($value) {
            return date('Y-m-d H:i:s', $value);
        }
    }

    public function getEscortReceiveTimeAttribute($value)
    {
        if($value) {
            return date('Y-m-d H:i:s', $value);
        }
    }
    public function getTypeTextAttribute()
    {
        return self::$types[$this->type];
    }
    public function getStatusTextAttribute()
    {
        return self::$statusEnum[$this->status];
    }

    public function school()
    {
        return $this->belongsTo(School::class, 'school_id', 'id')->select('id', 'name', 'address');
    }

    public function user()
    {
        return $this->belongsTo(User::class, 'uid', 'id');
    }
}
